package com.tads.torry.messagecenter.domain.accessor.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.tads.torry.messagecenter.domain.enums.ChannelType;
import com.tads.torry.messagecenter.domain.model.MessageSender;
import com.tads.torry.messagecenter.infras.entity.MessageSenderDO;
import com.tads.torry.messagecenter.infras.mapper.MessageSenderMapper;
import com.tads.torry.messagecenter.domain.accessor.MessageSenderAccessor;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.zen.boot.starter.mybatisplus.service.AccessorImpl;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author torry.wang
 * @since 2025-03-12
 */
@Service
@SuppressWarnings("DuplicatedCode")
public class MessageSenderAccessorImpl extends AccessorImpl<MessageSenderMapper, MessageSenderDO, MessageSender> implements MessageSenderAccessor {
    @Override
    public MessageSender toModel(MessageSenderDO entity) {
        if (entity == null) return null;
        MessageSender messageSender = new MessageSender();
        messageSender.setTenantId(entity.getTenantId());
        messageSender.setChannelType(StringUtils.isNotBlank(entity.getChannelType()) ? ChannelType.valueOf(entity.getChannelType()) : null);
        messageSender.setDescr(entity.getDescr());
        Map<String, Object> config = JSON.parseObject(entity.getConfig(), new TypeReference<Map<String, Object>>() {
        });
        messageSender.setConfig(config == null ? new HashMap<>() : config);
        messageSender.setId(entity.getId());
        messageSender.setCreateTime(entity.getCreateTime());
        messageSender.setUpdateTime(entity.getUpdateTime());
        return messageSender;
    }

    @Override
    public MessageSenderDO toEntity(MessageSender model) {
        if (model == null) return null;
        MessageSenderDO messageSenderDO = new MessageSenderDO();
        messageSenderDO.setTenantId(model.getTenantId());
        messageSenderDO.setChannelType(model.getChannelType() == null ? null : model.getChannelType().name());
        messageSenderDO.setDescr(model.getDescr());
        messageSenderDO.setConfig(MapUtils.isEmpty(model.getConfig()) ? null : JSON.toJSONString(model.getConfig()));
        messageSenderDO.setId(model.getId());
        messageSenderDO.setCreateTime(model.getCreateTime());
        messageSenderDO.setUpdateTime(model.getUpdateTime());
        return messageSenderDO;
    }

    public static void main(String[] args) {
        ChannelType channelType = ChannelType.valueOf("EMAIL");
        System.out.println(channelType);
        Map<String, Object> map = JSON.parseObject(null, new TypeReference<Map<String, Object>>() {
        });
        System.out.println(map);
        String s = JSON.toJSONString(null);
        System.out.println(s + "11");
    }
}
